//
//  ViewController.m
//  NSURLsession
//
//  Created by MyMac on 15/6/4.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<NSURLSessionDataDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   //[self requestURL:@"http://v.chuanke.com/vedio/1/08/65/10865711ff6997a671e6622352385208.mp4"];
    [self requestURL:@"http://img.blog.csdn.net/20140205183122750"];
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)requestURL:(NSString*)url
{
    NSURLSessionConfiguration*configuration=[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"11111"];
//NSURLRequest进行post请求
   NSURLRequest*request=[NSURLRequest requestWithURL:[NSURL URLWithString:url]];
  //  NSMutableURLRequest*requestURL=[[NSMutableURLRequest alloc]init];
  
    NSURLSession*session=[NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    
    
//  NSURLSessionDownloadTask*dataTask=  [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
//      //      UIImageView*image=[[UIImageView alloc]initWithFrame:self.view.bounds];
//      //      [self.view addSubview:image];
//      //      image.image=[UIImage imageWithData:data];
//      NSString*str=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
//      NSString*path=[str stringByAppendingPathComponent:@"14333.mp4"];
//      NSLog(@"%@ ********* %@  %@",location,response.MIMEType,configuration.URLCache);
//      NSData*data=[NSData dataWithContentsOfURL:location];
//      [data writeToFile:path atomically:YES];
//  }];//block在后台模式不可用
   NSURLSessionDownloadTask*dataTask=[session downloadTaskWithRequest:request];
    [dataTask resume];





}
//- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error;
//{
//    NSLog(@"1111");
//
//}
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
{

 NSLog(@"111333331");


}
//
//- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
//      didWriteData:(int64_t)bytesWritten
// totalBytesWritten:(int64_t)totalBytesWritten
//totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;
//{
//
//
//    NSLog(@"%ld     %ld     %ld",(long)bytesWritten,(long)totalBytesWritten,(long)totalBytesExpectedToWrite);
//
//}
- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error;
{

    NSLog(@"%@",error.description);


}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error;
{
NSString*str=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSLog(@")))))%@  %@",error.description,str);


}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location;
{

    NSString*str=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
          NSString*path=[str stringByAppendingPathComponent:@"1433300000"];
        NSLog(@"%@ ********* ]",location);
          NSData*data=[NSData dataWithContentsOfURL:location];
          [data writeToFile:path atomically:YES];

}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
      didWriteData:(int64_t)bytesWritten
 totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;
{
    NSLog(@"%ld",(long)bytesWritten);

}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
 didResumeAtOffset:(int64_t)fileOffset
expectedTotalBytes:(int64_t)expectedTotalBytes;
{

    NSLog(@"%ld",(long)fileOffset);


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
